<template>
  <div class="container">
    <div class="user-point">
      <div>当前积分</div>
      <div class="point">{{ userPoints }}</div>
      <div class="help">
        <img src="@/assets/kll/help.png" alt="" />
        积分规则
      </div>
    </div>
    <div class="top-nav">
      <div @click="tab = 0" :class="{ 'nav-cell': true, active: tab === 0 }">
        <span>日常任务</span>
      </div>
      <div @click="tab = 1" :class="{ 'nav-cell': true, active: tab === 1 }">
        <span>积分明细</span>
      </div>
    </div>
    <div v-if="tab === 0" class="daily-mission">
      <div class="mission-cell">
        <div class="mission-icon">
          <img class="small" src="@/assets/kll/shipin.png" alt="" />
        </div>
        <div class="mission-info">
          <div class="text">观看视频</div>
          <div class="point">+10积分</div>
        </div>
        <div @click="$router.push('/dynamics')" class="btn">
          去领取
          <img src="@/assets/车主宝首页改-assets/返回.png" alt="" />
        </div>
      </div>
      <div class="mission-cell">
        <div class="mission-icon">
          <img src="@/assets/kll/jifen.png" alt="" />
        </div>
        <div class="mission-info">
          <div class="text">阅读文章</div>
          <div class="point">+15积分</div>
        </div>
        <div
          @click="
            $router.push({
              name: 'dynamics',
              params: {
                action: 'read'
              }
            })
          "
          class="btn"
        >
          去领取
          <img src="@/assets/车主宝首页改-assets/返回.png" alt="" />
        </div>
      </div>
      <div class="mission-cell">
        <div class="mission-icon">
          <img src="@/assets/kll/zhuce.png" alt="" />
        </div>
        <div class="mission-info">
          <div class="text">邀请好友注册</div>
          <div class="point">+50积分</div>
        </div>
        <div @click="inviteRegister" class="btn">
          去领取
          <img src="@/assets/车主宝首页改-assets/返回.png" alt="" />
        </div>
      </div>
    </div>
    <div v-else class="point-detail">
      <div v-for="item in pointsList" :key="item.id" class="point-cell">
        <div class="left">
          <div class="text">{{ item.annotation }}</div>
          <div class="time">{{ item.created_at.date.split(" ")[0] }}</div>
        </div>
        <div class="right" :class="{ red: -item.number > 0 }">
          {{ item.number }}分
        </div>
      </div>
      <load-more @loadMore="loadMore"></load-more>
    </div>
  </div>
</template>

<script>
import loadMore from "@/components/load-more";
import { getUserPoint, getPointList } from "@/api/profile";
import { mapState } from 'vuex'
export default {
  data() {
    return {
      tab: 0,
      userPoints: 0,
      page: 1,
      pointsList: [],
      loading: false,
      isAll: false
    };
  },

  computed: {
    ...mapState({
      token: state => state.user.token,
      mainColor: state => state.app.mainColor,
      vip_rule: state => state.app.vip_rule,
    })
  },

  watch: {},

  methods: {
    inviteRegister() {
      if (this.vip_rule === 1) {
        this.$router.push('/vip-invite')
      } else if (this.vip_rule === 0) {
        this.$router.push('/Vip')
      }
      
    },
    loadMore() {
      if (this.loading || this.isAll || this.pointsList.length === 0) return;
      this.loading = true;
      this.getPointList(++this.page);
    },
    getPointList(page) {
      getPointList({ page }).then(res => {
        if (res.status === 200) {
          this.pointsList.push(...res.data.data);
          if (res.data.data.length === 0) {
            this.isAll = true;
          }
        }
        this.loading = false;
      });
    }
  },

  created() {
    this.getPointList(1);
    getUserPoint().then(res => {
      if (res.status === 200) {
        this.userPoints = res.data.data.points;
      }
    });
  },

  components: {
    loadMore
  }
};
</script>
<style lang="less" scoped>
.container {
  min-height: 100%;
  height: auto;
  background: #fff;
  // padding-bottom: 1rem;
  .user-point {
    background-image: url("../../assets/kll/beijin.png");
    background-size: 100% 100%;
    background-repeat: no-repeat;
    height: 3.3rem;
    margin: 0.3rem;
    padding-left: 5rem;
    font-size: 0.6rem;
    color: #fff;
    padding-top: 0.8rem;
    position: relative;
    .point {
      font-size: 0.88rem;
      font-weight: bold;
    }
    .help {
      display: flex;
      align-items: center;
      position: absolute;
      right: 0.48rem;
      top: 0.48rem;
      color: #fff;
      font-size: 0.52rem;
      img {
        width: 0.7rem;
        margin-right: 0.2rem;
      }
    }
  }
  .top-nav {
    background: #fff;
    height: 1.75rem;
    display: flex;
    color: #333;
    font-size: 0.52rem;
    padding-left: 0.48rem;
    .nav-cell {
      width: 3.2rem;
      text-align: center;
      margin: 0.5rem 0.5rem 0;
      transition-duration: 0.1s;
      line-height: 1rem;
      span {
        transition-duration: 0.2s;
        display: inline-block;
        height: 0.8rem;
      }
    }

    .active {
      font-weight: bold;
      font-size: 0.72rem;
      border-bottom: 0.12rem solid rgba(0, 120, 255, 1);
    }
  }
  .daily-mission {
    .mission-cell {
      background: rgba(247, 246, 249, 1);
      border-radius: 0.2rem;
      height: 3.3rem;
      display: flex;
      align-items: center;
      margin: 0.24rem 3.2% 0;
      .mission-icon {
        width: 3.2rem;
        text-align: center;
        img {
          width: 1.36rem;
        }
        .small {
          width: 1rem;
        }
      }
      .mission-info {
        font-size: 0.48rem;
        color: #333;
        line-height: 1rem;
        width: calc(100% - 3.2rem - 2.8rem);
        .point {
          color: #c89f64;
        }
      }
      .btn {
        width: 2.8rem;
        display: flex;
        align-items: center;
        font-size: 0.48rem;
        color: #3393ff;
        img {
          width: 0.2rem;
          margin-left: 0.48rem;
        }
      }
    }
  }
  .point-detail {
    .point-cell {
      padding: 0.24rem 3.2%;
      display: flex;
      font-size: 0.6rem;
      justify-content: space-between;
      align-items: center;
      border-bottom: 0.02rem solid #dfdfdf;
      .left {
        line-height: 1.2rem;
        .left {
          color: #333;
        }
        .time {
          color: #666;
        }
      }
      .right {
        color: #3393ff;
      }
      .red {
        color: #ee2c30;
      }
    }
  }
}
</style>
